package v1

import (
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"pq/models"
	"strings"
	"time"
)

type ExtraController struct {
	BaseController
}

func (ec *ExtraController) SendVerify()  {
	//code : ec.Ctx
	slave := models.GetSlaveDb()
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
	vcode := fmt.Sprintf("%05v", rnd.Int31n(1000000))
	phone := ec.GetString("phone")
	stype,_ := ec.GetInt("stype")
	master := models.GetMasterDb()
	download  := models.Download{}
	_ = slave.QueryTable(`download`).Filter("phone", phone).Filter("stype", stype).One(&download)
	download.VerifyCode = vcode
	download.City = ec.GetString("city")
	download.Name = ec.GetString("name")
	download.Phone = ec.GetString("phone")
	download.Stype,_ = ec.GetInt("stype")
	var id int64
	if download.Id > 0{
		_, _ = master.Update(&download)
		id = download.Id
	} else{
		id,_ =  master.Insert(&download)
	}
	res := map[string]interface{}{}
	res["id"] = id
	//TODO:post to weimi
	content := fmt.Sprintf("uid=%s&pas=%s&mob=%s&cid=%s&p1=%s&type=json","Iy9iEnm0jPWp","xktsj5kr",download.Phone,"ADe2O1D0rH0o",vcode)
	resp, err := http.Post("http://api.weimi.cc/2/sms/send.html",
		"application/x-www-form-urlencoded",
		strings.NewReader(content))
	if err != nil {
		fmt.Println(err)
	} else{
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err == nil {
			fmt.Println(string(body))
		}
	}
	ec.Single(res)
}
func (ec *ExtraController)SendBusVerify(){
	slave := models.GetSlaveDb()
	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
	vcode := fmt.Sprintf("%05v", rnd.Int31n(1000000))
	phone := ec.GetString("phone")
	stype,_ := ec.GetInt("stype")
	master := models.GetMasterDb()
	download  := models.Download{}
	slave.QueryTable(`download`).Filter("phone",phone).Filter("stype",stype).One(&download)
	download.VerifyCode = vcode
	download.City = ec.GetString("city")
	download.Name = ec.GetString("name")
	download.Phone = ec.GetString("phone")
	download.Stype,_ = ec.GetInt("stype")
	download.Place = ec.GetString("place")
	var id int64
	if download.Id > 0{
		master.Update(&download)
		id = download.Id
	} else{
		id,_ =  master.Insert(&download)
	}
	res := map[string]interface{}{}
	res["id"] = id
	//TODO:post to weimi
	content := fmt.Sprintf("uid=%s&pas=%s&mob=%s&cid=%s&p1=%s&type=json","Iy9iEnm0jPWp","xktsj5kr",download.Phone,"wjuqlrrgv0Wq",vcode)
	resp, err := http.Post("http://api.weimi.cc/2/sms/send.html",
		"application/x-www-form-urlencoded",
		strings.NewReader(content))
	if err != nil {
		fmt.Println(err)
	} else{
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
		if err == nil {
			fmt.Println(string(body))
		}
	}
	ec.Single(res)
}
func (ec *ExtraController) Save(){
	vcode := ec.GetString("code")
	id,_    := ec.GetInt("id")
	slave := models.GetSlaveDb()
	master := models.GetMasterDb()
	download := models.Download{}
	slave.QueryTable(`download`).Filter("id",id).One(&download)

	if download.Id == 0 || vcode != download.VerifyCode{
		ec.Fail(`Fail to verify`,`fail`)
	}
	download.City = ec.GetString("city")
	master.Update(&download)
	ec.Success(`Success`)
}